package cn.com.citydo.controller;

import cn.com.citydo.common.core.universal.exception.SysException;
import cn.com.citydo.dto.TestDTO;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *对外开放应用接口实例
 *  * @date 2025-03-21 17:07
 * @date 2025-03-21 17:07
 */
@RestController()
@RequestMapping("/demo/api")
public class GatewayController {

    private final Map<String, String> dataMap = new ConcurrentHashMap<>();
    @PostMapping
    public String add(@RequestBody TestDTO testDTO) {
        if (StringUtils.isBlank(testDTO.getKey()) || StringUtils.isBlank(testDTO.getValue())) {
            throw new SysException("key 和value都要必填");
        }
        dataMap.put(testDTO.getKey(), testDTO.getValue());
        return JSON.toJSONString(testDTO);
    }

    @PutMapping
    public String update(@RequestBody TestDTO testDTO) {

        if (StringUtils.isBlank(testDTO.getKey()) || StringUtils.isBlank(testDTO.getValue())) {
            throw new SysException("key 和value都要必填");
        }
        dataMap.put(testDTO.getKey(), testDTO.getValue());
        return JSON.toJSONString(testDTO);
    }

    @GetMapping
    public String query(@RequestParam(value = "key") String key) {

        if (StringUtils.isBlank(key)) {
            throw new SysException("key 要必填");
        }
        return dataMap.get(key);
    }

    @DeleteMapping
    public String delete(@RequestParam(value = "key") String key) {

        if (StringUtils.isBlank(key)) {
            throw new SysException("key 要必填");
        }
        String remove = dataMap.remove(key);
        if(null==remove){
            return "";
        }
        return remove;
    }


}
